<template>
  <div>
    <!-- <div class="page-tool">
      <div class="page-tool-item" @click="lastPage">上一页</div>
      <div class="page-tool-item" @click="nextPage">下一页</div>
      <div class="page-tool-item">第{{state.pageNum+ '/'+ state.numPages}}页</div>
      <div class="page-tool-item" @click="pageZoomOut">放大</div>
      <div class="page-tool-item" @click="pageZoomIn">缩小</div>
  </div>
  <div style="width: 100%;
    height: 90vh;
    overflow: scroll;">
    <vue-pdf-embed :source="state.source" :scale="state.scale"  :style="scaleFun" class="vue-pdf-embed" :page="state.pageNum" />
  </div> -->
  <div id="pdf" v-show="true"></div>
</div>
</template>
<script setup>
  import { onMounted } from 'vue'
  import { ElLoading } from 'element-plus'
  import Pdfh5 from "pdfh5";
  // import VuePdfEmbed from "vue-pdf-embed";
  // import { createLoadingTask } from "vue3-pdfjs/esm"; // 获得总页数
  import { useRoute } from "vue-router";
  const route= useRoute()



onMounted(() => {
  let pdfs = new Pdfh5("#pdf", {
  pdfurl: route.query.pdfUrl,
  // pdfurl: '/public/2.pdf',
  lazy: false, //是否开启懒加载
  maxZoom: 3, //手势缩放最大倍数 默认3
  scrollEnable: true, //是否允许pdf滚动
  zoomEnable: true, //是否允许pdf手势缩放
  renderType: 'svg',
  backTop: true,
  scale: 1,
  cMapUrl: 'https://unpkg.com/pdfjs-dist@2.0.943/cmaps/',
});
pdfs.on("complete", function (status, msg, time) {
    // console.log("状态：" + status + "，信息：" + msg + "，耗时：" + time + "毫秒，总页数：" + this.totalNum);
    state.numPages = this.totalNum;
    clearTimeout(setTime)
    setTime = null
    loading.close()
  });
})

const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0, 0, 0, 0.7)',
  })
let setTime = null
setTime = setTimeout(() => {
  loading.close()
}, 30000)

  document.title = '查看PDF';
  const state = reactive({
    // source:  route.query.pdfUrl, //预览pdf文件地址
    // source: './932c2218-f614-40a4-954b-3f652e38011a.pdf',
    // source: '/1.pdf',
    pageNum: 1, //当前页面
    scale: 1, // 缩放比例
    numPages: 0, // 总页数
});



const scaleFun = computed(() => {
  var scale = state.scale;
  return `transform:scale(${scale})`;
})

const lastPage = () => {
    if (state.pageNum > 1) {
        state.pageNum -= 1;
    }
}

const nextPage = () => {
    if (state.pageNum < state.numPages) {
        state.pageNum += 1;
    }
}
const pageZoomOut = ()  =>  {

  state.scale += 0.1;
}
const pageZoomIn = () => {
    if (state.scale > 1) {
        state.scale -= 0.1;
    }
}

// const loadingTask = createLoadingTask(state.source);
//    loadingTask.promise.then((pdf) => {
//        state.numPages = pdf.numPages;
//    });



</script>
<style scoped>
  .pdf-preview {
    position: relative;
    height: 100vh;
    padding: 20px 0;
    box-sizing: border-box;
    background-color: e9e9e9;
}
.pdf-wrap{
    overflow-y:auto ;
}
.vue-pdf-embed {
    text-align: center;
    width: 515px;
    border: 1px solid #e5e5e5;
    margin: 0 auto;
    box-sizing: border-box;
}
 
.page-tool {
    position: absolute;
    bottom: 35px;
    padding-left: 15px;
    padding-right: 15px;
    display: flex;
    align-items: center;
    background: rgb(66, 66, 66);
    color: white;
    border-radius: 19px;
    z-index: 100;
    cursor: pointer;
    margin-left: 50%;
    transform: translateX(-50%);
    font-size: 12px;
}
.page-tool-item {
    padding: 8px 15px;
    padding-left: 10px;
    white-space:nowrap;
    cursor: pointer;
}
#pdf {
  width: 100%;
  height: 100vh;
  overflow: scroll;
}
</style>